package com.yeyaomai.dksns.flowis.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.Properties;


public class StringUtil {

	/**
	 * 计算百分比 返回 带矢量图片
	 */
	public static String controlLength(String s1, String s2)
			throws UnsupportedEncodingException {

		String resuit = "";
		if (s1 != null && !s1.equals("0") && s2 != null && !s2.equals("0")) {

			float f1 = Float.valueOf(s1);
			float f2 = Float.valueOf(s2);
			resuit = String.valueOf(new BigDecimal(f1 / f2 * 100).setScale(2,
					BigDecimal.ROUND_HALF_UP).toString());
			resuit = "<img src='../images/blue_bar.gif' width='"
					+ new BigDecimal(f1 / f2 * 100).setScale(0,
							BigDecimal.ROUND_UP) + "' height='10'>" + resuit
					+ "%";
		} else {
			resuit = "<img src='../images/blue_bar.gif' width='" + 1
					+ "' height='10'>" + 0.00 + "%";
		}

		return resuit;

	}

	/**
	 * 
	 * @return 计算百分比
	 */

	public static String proportion(String s1, String s2, String scale)
			throws UnsupportedEncodingException {

		String resuit = "0";
		if (s1 != null && !s1.equals("0") && s2 != null && !s2.equals("0")) {

			float f1 = Float.valueOf(s1);
			float f2 = Float.valueOf(s2);
			float f3 = Float.valueOf(scale);
			resuit = String.valueOf(new BigDecimal(f1 / f2 * f3).setScale(2,
					BigDecimal.ROUND_HALF_UP).toString());
		}
		return resuit;

	}

	public static String getPath(String path, int number) {
		String url = "";
		if (path.length() > number) {
			url = path.substring(0, number) + "...";
		} else {
			url = path;
		}
		return url;
	}

	public static String getDouble(int x, int y, int total) {
		float ret = (100 * (float) x) / (float) y;
		return String.format("%.2f", ret);
	}
	
	public static String getPageName(String pageName) throws UnsupportedEncodingException
	{
		return URLEncoder.encode(pageName, "UTF-8");
	}
 
	 public static String getModule(String moduleNum) throws IOException
	 {
		 
		 Properties props = new Properties();
		 //	InputStream in=new FileInputStream(new File("module.properties"));
		 InputStream in=StringUtil.class.getClassLoader().getResourceAsStream("module.properties");
		 BufferedReader bf=new BufferedReader(new InputStreamReader(in));  
		  props.load(bf);
         String aa = props.getProperty(moduleNum);
		 return aa;
	 }
	 public   static void main(String[] args) throws IOException {
		 //test case...
//		 System.out.println(getModule("1"));
	}
}
